﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;

namespace ISchool
{
    class ISaveOrSaveAs
    {
        //作者：郭菲
        //描述；针对系统扩展功能保存和另存地图文档(格式：Mxd)
        private AxMapControl m_mapControl;
        private bool isSucess = false;

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="mu"></param>
        public ISaveOrSaveAs(AxMapControl mu)
        {
            m_mapControl = mu;
        }
        /// <summary>
        /// 直接默认保存方式
        /// </summary>
        /// <param name="filename"></param>
        public void mSave()
        {
            if (!isEmpty())
            {
                //地图转换成文档内容
                IMxdContents pmxd = m_mapControl.Map as IMxdContents;
                //新建文档操作的方法
                IMapDocument pdot = new MapDocumentClass();
                //将地图内容打开到文档中去
                pdot.Open(m_mapControl.DocumentFilename, "");
                //取代内容
                pdot.ReplaceContents(pmxd);
                //存储
                pdot.Save(true, true);
                MessageBox.Show("直接存储成功");
            }
            else
                MessageBox.Show("无法保存空地图");
        }
        /// <summary>
        /// 文档另存为
        /// </summary>
        public void mSaveAs()
        {
            if (!isEmpty())
            {
                try
                {
                    //打开文档选项对话框
                    SaveFileDialog Sfd = new SaveFileDialog();
                    //设置参数
                    Sfd.Title = "另存文档";
                    Sfd.Filter = "(*.mxd)|*.mxd";
                    Sfd.ShowDialog();
                    //获取文件保存名和路径
                    string path = Sfd.FileName;
                    //新建文档内容
                    IMxdContents pMxdC;
                    pMxdC = m_mapControl.Map as IMxdContents;
                    //文档内容操作方法
                    IMapDocument pMapDocument = new MapDocumentClass();
                    //新建文档
                    pMapDocument.New(path);
                    //内容替换
                    pMapDocument.ReplaceContents(pMxdC);
                    //保存
                    pMapDocument.Save(true, true);
                    MessageBox.Show("另存成功");
                }
                catch (IOException e)
                {
                    if (e.Source != null)
                        MessageBox.Show("IOException source:{0}", e.Source);
                    throw;
                }
            }
            else
                MessageBox.Show("无法保存空地图");
        }
        public bool isEmpty()
        {
            if (m_mapControl.Map == null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 检查文件的可用性
        /// </summary>
        /// <returns></returns>
        public bool isRight()
        {
            return true;
        }
    }
}
